<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="会员姓名">
              <el-input v-model="form.realname" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员手机号">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="证件号码">
              <el-input v-model="form.identity" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="证件类型">
              <el-select v-model="form.identityType" placeholder="请选择">
                <el-option
                  v-for="item in cardOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="form.reviewStatus" placeholder="请选择">
                <el-option
                  v-for="item in reviewOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="realname" label="会员姓名" />
        <el-table-column prop="telephone" label="会员手机号">
          <template slot-scope="scope">
            <div>{{scope.row.telephone | phonereg }}</div>
          </template>
        </el-table-column>
        <el-table-column label="证件类型">
          <template slot-scope="scope">{{ scope.row.identityType | cardType }}</template>
        </el-table-column>
        <el-table-column prop="identity" label="证件号码"></el-table-column>
        <el-table-column label="证件正面">
          <template slot-scope="scope">
            <img
              style="height:60px"
              :src="scope.row.identityImg"
              alt
              @click="diafun(scope.row.identityImg)"
            />
          </template>
        </el-table-column>
        <el-table-column label="证件反面">
          <template slot-scope="scope">
            <img
              style="height:60px"
              :src="scope.row.identityBackImg"
              alt
              @click="diafun(scope.row.identityBackImg)"
            />
          </template>
        </el-table-column>
        <el-table-column label="审核状态">
          <template slot-scope="scope">{{ scope.row.reviewStatus | RS }}</template>
        </el-table-column>
        <el-table-column prop="reviewRemark" label="审核驳回标注" />
        <el-table-column prop="createdAt" label="提交时间" />
        <el-table-column
          v-if="hasPermission('AdminUserIdentityAuthProvider.review')"
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <!-- <el-button v-if="!scope.row.reviewStatus" type="text" @click="auditFn(scope.row.userId)" size="small">审核</el-button>
            <span v-else style="font-size:12px">已审核</span>-->
            <el-button
              v-if="!scope.row.reviewStatus"
              type="text"
              size="small"
              @click="auditBtn(scope.row)"
            >审核</el-button>
            <span v-else style="font-size:12px">已审核</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 审核弹窗 -->
      <el-dialog title="审核" :visible.sync="dialog" :before-close="aduitClose" width="468px">
        <!-- 输入框 -->
        <el-form :inline="true" label-width="100px" label-position="right">
          <el-row>
            <el-col :span="24">
              <el-form-item label="审核状态">
                <el-select v-model="aduitForm.reviewStatus" placeholder="请选择">
                  <el-option
                    v-for="item in rwOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="用户手机号">
                <span class="spanToInput">{{ dialogForm.telephone }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="flag" label="用户姓名">
                <!-- <span class="spanToInput">{{dialogForm.realname}}</span> -->
                <el-input v-model="aduitForm.realname" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="flag" label="证件号码">
                <!-- <span class="spanToInput">{{dialogForm.identity}}</span> -->
                <el-input v-model="aduitForm.identity" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="!flag" label="驳回原因">
                <el-input v-model="aduitForm.reviewRemark" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证件正面">
                <img
                  style="height:60px"
                  :src="dialogForm.identityImg"
                  alt
                  @click="diafun(dialogForm.identityImg)"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证件反面">
                <img
                  style="height:60px"
                  :src="dialogForm.identityBackImg"
                  alt
                  @click="diafun(dialogForm.identityBackImg)"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="aduitClose()">取 消</el-button>
          <el-button type="primary" @click="aduitSubmit()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog :visible.sync="dialogVisible" width="30%">
      <img :src="imgback" style="width:100%" alt />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  certificationList,
  review,
  modifyUserServerStatus
} from "@/api/certificationList";

export default {
  name: "AuditHMT",
  filters: {
    cardType: function(e) {
      return window.helper.cardType(String(e));
    },
    // 0为未审核 1为审核通过  2为审核驳回
    RS: function(e) {
      switch (e) {
        case 0:
          return "未审核";
        case 1:
          return "审核通过";
        case 2:
          return "审核驳回";
        default:
          return "暂无";
      }
    }
  },
  data() {
    return {
      imgback: "",
      dialogVisible: false,
      dialog: false,
      tableLoading: true,
      form: {
        realName: "",
        username: "",
        identity: "",
        identityType: "",
        reviewStatus: null,
        pageNum: 1,
        pageSize: 10
      },
      aduitForm: {
        reviewStatus: 1,
        userId: null
      },
      dialogForm: {},
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        identityType: "",
        pageNum: 1,
        pageSize: 10
      },
      cardOptions: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "1",
          label: "港澳通行证"
        },
        {
          value: "2",
          label: "台湾身份证"
        },
        {
          value: "3",
          label: "护照"
        }
      ],
      reviewOptions: [
        {
          value: -1,
          label: "全部"
        },
        {
          value: 0,
          label: "未审核"
        },
        {
          value: 1,
          label: "审核通过"
        },
        {
          value: 2,
          label: "审核驳回"
        }
      ],
      rwOptions: [
        {
          value: 1,
          label: "审核通过"
        },
        {
          value: 2,
          label: "审核驳回"
        }
      ],
      flag: true
    };
  },
  watch: {
    "aduitForm.reviewStatus"(val) {
      if (val == 1) {
        this.flag = true;
      } else {
        this.flag = false;
      }
    }
  },
  mounted() {
    this.getList(this.listForm);
  },
  methods: {
    diafun(src) {
      this.imgback = src;
      this.dialogVisible = true;
    },
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 审核弹窗按钮
    auditBtn(e) {
      this.dialogForm = e;
      console.log(e);
      this.aduitForm.userId = e.userId;
      this.dialog = true;
    },
    // 审核关闭按钮
    aduitClose() {
      this.dialog = false;
    },
    // 审核提交方法
    aduitSubmit() {
      console.log(this.aduitForm);
      const datas = this.aduitForm;
      // console.log(JSON.stringify(datas))
      review(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("审核");
            this.getList(this.listForm);
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });

      this.aduitForm = {
        reviewStatus: 1,
        userId: null
      };
      this.dialog = false;
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      this.getList(this.form);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      certificationList(datas)
        .then(res => {
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    // 审核
    auditFn(e) {
      this.$confirm("是否审核通过？", "提示", {
        confirmButtonText: "审核通过",
        cancelButtonText: "驳回",
        type: "warning"
      })
        .then(() => {
          const datas = {
            userId: e,
            reviewStatus: "1",
            reviewRemark: ""
          };
          console.log(JSON.stringify(datas));
          review(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("审核");
              this.getList(this.listForm);
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          const datas = {
            userId: e,
            reviewStatus: "2",
            reviewRemark: ""
          };
          review(datas).then(res => {
            if (!res.data.code) {
              this.$message({
                type: "info",
                message: "已驳回"
              });
              this.getList(this.listForm);
            } else {
              this.$message.error(res.data.msg);
            }
          });
        });
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
